package models

import "gorm.io/gorm"

type Message struct{
	gorm.Model
	key string  `gorm:"unique_index;not null"`    //主键
	Content string
	UserId int
	User User
	NodeKey string                          //评论key
	Praise int   `gorm:"default:0"`          //点赞
}

func SaveMessage(message *Message)error{
      res:=db.Create(message)
      return res.Error    //创建数据
}

func QueryMessageByKey(key string)(num int64,messages []Message,err error){
	res:=db.Preload("User").Where("node_key=?",key).Find(&messages)
	return res.RowsAffected,messages,res.Error
}

func QueryAllMessage(pageno int,pagesize int)(num int,messages []Message,err error){
	res:=db.Preload("User").Where("node_key = ''").Offset((pageno-1)*pagesize).Limit(pagesize).Order("updated_at desc").Find(&messages)
	return int(res.RowsAffected),messages,res.Error
}

func QueryAllMessageCount()(int,error){
	var num int64
	res:=db.Model(&Message{}).Where("node_key = ''").Count(&num)
	return int(num),res.Error
}